package com.anzia.stream;

import java.io.FileInputStream;

/**
 *@ClassName com.anzia.stream.FileInputStreamDemo
 *@Description 演示FileInputStream的使用，文件字节输入流
 *@Author Anzia.
 *@Date 2021/4/8 20:45
 *@Version 1.0
 **/
@SuppressWarnings("ALL")
public class FileInputStreamDemo {

    public static void main(String[] args) throws Exception {

        //创建FileInputStream, 并指定文件路径
        FileInputStream fis = new FileInputStream("static\\test_in.txt");
        FileInputStreamDemo demo1 = new FileInputStreamDemo();

        //读取文件
        //demo1.single_read(fis);
        //demo1.more_read1(fis);
        demo1.more_read2(fis);

        fis.close();
        System.out.println();
        System.out.println("执行完毕!");

    }

    //单个字节读取
    public void single_read(FileInputStream fis) throws Exception {

        int data = 0;
        //fis.read() 读取完毕后，返回值是 -1
        while ((data = fis.read()) != -1) {
            //data 是整形，要强转换 char
            System.out.print((char) data);
        }

    }

    public void more_read1(FileInputStream fis) throws Exception {

        byte[] bytes = new byte[3];

        int count = fis.read(bytes);
        System.out.println(new String(bytes));
        System.out.println(count);

        int count2 = fis.read(bytes);
        System.out.println(new String(bytes));
        System.out.println(count2);

        int count3 = fis.read(bytes);
        System.out.println(new String(bytes,0,count3));
        System.out.println(count3);

    }

    public void more_read2(FileInputStream fis) throws Exception {

        byte[] bytes = new byte[3];
        int count = 0;
        while ((count = fis.read(bytes)) != -1) {
            System.out.println(new String(bytes,0,count));
        }

    }
}
